package org.springcloud.wisdom.spring_02_classroom.teacher.records.controller;

import org.springcloud.wisdom.spring_02_classroom.bean.BaseController;
import org.springcloud.wisdom.spring_02_classroom.bean.ResponseData;
import org.springcloud.wisdom.spring_02_classroom.teacher.records.entity.RecordsEntity;
import org.springcloud.wisdom.spring_02_classroom.teacher.records.service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/sign")
public class RecordsController extends BaseController {
    @Autowired
    private RecordsService recordsService;

    // 发起签到
    @PostMapping("/ok")
    public ResponseData<Boolean> insertRecords(@RequestBody RecordsEntity recordsEntity) {
        String tid = recordsEntity.getTid();
        String cid = recordsEntity.getCid();
        boolean result = recordsService.insertRecordsAndAttendence(tid, cid);
        return returnBoolean(result);
    }

    // 学生签到
    @PutMapping("/success")
    public ResponseData<Object> updateRecords(@RequestBody RecordsEntity recordsEntity) {
        boolean success = recordsService.updateRecordStatus(recordsEntity);
        return returnObject(success ? "签到成功！" : "签到失败！");
    }

    // 获取签到记录
    @RequestMapping("/all")
    public ResponseData<Object> getPage(@RequestParam("pageNum") int pageNum,
                                        @RequestParam("pageSize") int pageSize,
                                        @RequestParam("sid") String sid){
        return returnObject(recordsService.getAllRecords(pageNum, pageSize, sid));
    }
}